package com.fuxi.cloud.desreport.render.a;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.fuxi.cloud.common.constant.ExpConstant;
import com.fuxi.cloud.desreport.model.RenderInfo;
import com.fuxi.cloud.desreport.model.ReportDbInfo;
import com.fuxi.cloud.desreport.render.handler.RowRenderHandler;
import com.fuxi.cloud.desreport.render.utils.FreeMarkerUtils;
import com.fuxi.cloud.desreport.render.utils.RegexMatches;
import com.fuxi.cloud.desreport.render.utils.RenderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component("tableRenderStrategy")
public class d implements RowRenderHandler {
    private static final Logger a = LoggerFactory.getLogger(d.class);

    @Override
    public boolean support(JSONObject row) {
        return RenderUtil.a(row, new String[] { "${" }, new String[] { "group(" }).booleanValue();
    }

    @Override
    public String getTemplate(String content, Integer rowKey, String dataSetKey, JSONObject currentRow, JSONObject lastRow) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{");
        stringBuilder.append(String.format("<#list list as %s>", new Object[] { dataSetKey }));
        stringBuilder.append(String.format("\"${%s_index+%s}\":", new Object[] { dataSetKey, rowKey }));
        stringBuilder.append(content);
        stringBuilder.append(String.format("<#if %s_has_next>,</#if>", new Object[] { dataSetKey }));
        stringBuilder.append("</#list>");
        stringBuilder.append("}");
        return stringBuilder.toString();
    }

    @Override
    public JSONObject render(RenderInfo renderInfo, JSONObject rows, JSONObject currentRow, Integer rowIndex, Integer dynamicRowEndIndex) {
        Map var6 = renderInfo.getReport().getDataList();
        String var7 = currentRow.toString();
        HashMap var8 = new HashMap(5);
        String var9 = this.getDataSetKey(var7);
        JSONObject var10 = (JSONObject)rows.get(rowIndex - 1);
        if (ObjectUtil.isNotEmpty(var9)) {
            ReportDbInfo var11 = (ReportDbInfo)var6.get(var9);
            if (ObjectUtil.isNotEmpty(var11)) {
                Object var12 = this.getData(renderInfo, var11);
                var8.put("list", var12);
                currentRow = RenderUtil.a(currentRow, (var2, var3) -> {
                    Set var4 = RegexMatches.a(var3);
                    String var61;
                    Integer var71;
                    if (ObjectUtil.isNotEmpty(var4)) {
                        for(Iterator var5 = var4.iterator(); var5.hasNext(); var3 = var3.replace(var61, String.format("${%s_index+%s}", var9, var71))) {
                            var61 = (String)var5.next();
                            var71 = dynamicRowEndIndex + 1;
                        }
                    }

                    return var3;
                }, ExpConstant.FUNCTION);
                String var13 = this.getTemplate(currentRow.toString(), dynamicRowEndIndex, var9, currentRow, var10);
                String var14 = FreeMarkerUtils.a(var13, var8);
                var14 = a(var14);
                JSONObject var15 = JSONObject.parseObject(var14, new Feature[]{Feature.OrderedField});
                this.afterRenderRow(renderInfo, currentRow, var15, dynamicRowEndIndex);
                return var15;
            } else {
                return currentRow;
            }
        } else {
            return null;
        }
    }

    @Override
    public Object getData(RenderInfo renderInfo, ReportDbInfo reportDbInfo) {
        return reportDbInfo.getList();
    }

    public JSONObject a(RenderInfo paramRenderInfo, JSONObject paramJSONObject) {
        HashMap<String, JSONObject> hashMap = new HashMap<>(5);
        for (Map.Entry entry : paramJSONObject.entrySet()) {
            String str = entry.getKey().toString();
            JSONObject jSONObject = (JSONObject)entry.getValue();
            for (Map.Entry entry1 : jSONObject.entrySet()) {
                Object object = entry1.getValue();
                JSONObject jSONObject1 = new JSONObject();
                if (object instanceof JSONObject) {
                    JSONObject jSONObject2 = (JSONObject)object;
                    String str1 = "";
                    for (Map.Entry entry2 : jSONObject2.entrySet()) {
                        String str2 = entry2.getKey().toString();
                        JSONObject jSONObject3 = (JSONObject)entry2.getValue();
                        if (ObjectUtil.isNotEmpty(jSONObject3)) {
                            String str3 = jSONObject3.getString("text");
                            String str4 = jSONObject3.getString("aggregate");
                            if (ObjectUtil.isNotEmpty(str4) && "group".equals(str4.toString())) {
                                str1 = str1 + str3 + ":";
                                JSONObject jSONObject4 = a(paramRenderInfo, (Map)hashMap, jSONObject3, str1, str2, str);
                                if (ObjectUtil.isNotEmpty(jSONObject4))
                                    jSONObject1.put(str2, jSONObject4);
                                continue;
                            }
                            jSONObject1.put(str2, jSONObject3);
                        }
                    }
                    jSONObject.put("cells", jSONObject1);
                }
            }
            paramJSONObject.put(str, jSONObject);
        }
        JSONArray jSONArray = RenderUtil.a(hashMap);
        paramRenderInfo.addMerges(jSONArray);
        return paramJSONObject;
    }

    public JSONObject a(RenderInfo paramRenderInfo, Map<String, JSONObject> paramMap, JSONObject paramJSONObject, String paramString1, String paramString2, String paramString3) {
        JSONObject jSONObject = paramMap.get(paramString1);
        if (ObjectUtil.isNotEmpty(jSONObject)) {
            JSONArray jSONArray = jSONObject.getJSONArray("merge");
            jSONObject.put("cellIndex", paramString2);
            jSONObject.put("rowIndex", paramString3);
            if (ObjectUtil.isEmpty(jSONArray)) {
                Integer[] arrayOfInteger = { Integer.valueOf(1), Integer.valueOf(0) };
                jSONObject.put("merge", arrayOfInteger);
                paramMap.put(paramString1, jSONObject);
            } else {
                Integer[] arrayOfInteger1 = (Integer[])jSONArray.toArray((Object[])new Integer[0]);
                Integer arrayOfInteger2[] = arrayOfInteger1, integer1 = arrayOfInteger2[0], integer2 = arrayOfInteger2[0] = Integer.valueOf(arrayOfInteger2[0].intValue() + 1);
                jSONObject.put("merge", arrayOfInteger1);
                paramMap.putIfAbsent(paramString1, jSONObject);
                paramJSONObject.clear();
            }
        } else {
            paramMap.put(paramString1, paramJSONObject);
            return paramJSONObject;
        }
        return null;
    }

    private static String a(String var0) {
        char[] var1 = var0.toCharArray();
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            if (var1[var3] == ':' && var1[var3 + 1] == '"') {
                for(int var4 = var3 + 2; var4 < var2; ++var4) {
                    if (var1[var4] == '"') {
                        if (var1[var4 + 1] != ',' && var1[var4 + 1] != '}') {
                            var1[var4] = ' ';
                        } else if (var1[var4 + 1] == ',' || var1[var4 + 1] == '}') {
                            break;
                        }
                    }
                }
            }
        }

        return new String(var1);
    }
}
